﻿using System;
using System.Windows.Forms;
using Interfaces.Information_Interfaces;
using Interfaces.Add_Interfaces;
using Interfaces.Update_Interfaces;
using Interfaces.Delete_Interfaces;
using Interfaces.General_Interfaces;
using Interfaces.Presenter_Interfaces;
using Presenters.Inherited_Presenters;
using Repository;
using Model.Domain_Model;

namespace Presenters.General_Presenters
{
    public class ClientsViewPresenter:OperationStatus
    {
        public event EventHandler<EventArgs> ActionToParentWindow;
        private IClientsView view = null;
        private IDb database = Repo.GetDatabaseInstance();
        private delegate void PrepareGrid();
        private PrepareGrid clientListGrid;
        public Client selectedClient = null;

        public ClientsViewPresenter(IClientsView view)
        {
            this.view = view;
            clientListGrid = new PrepareGrid(view.MappingColumnName);
            clientListGrid += view.ResizeColumn;
            clientListGrid += view.HideColumn;
            Configure();
        }

        private void Configure()
        {
            ActionToParentWindow += GetClientList;
            view.GetClientList += GetClientList;
            view.Search += SearchClient;
        }

        public void SearchClient(object sender, EventArgs args)
        {
            nameOperation = "Wyszukaj klienta";
            try
            {
                view.clientList = database.Clients.
                    SearchClient(inventedName: view.inventedName, nameCorp: view.nameCorporation);
                clientListGrid();
            }
            catch (Exception ex)
            {
                exception = ex.Message;
                view.ShowError();
            }
        }

        public void GetClientList(object sender, EventArgs args)
        {
            nameOperation = "Lista klientów";
            try
            {
                view.clientList = database.Clients.GetClientList();
                clientListGrid();
            }
            catch (Exception ex)
            {
                exception = ex.Message;
                view.ShowError();
            }
        }

        public void DisplayView(object viewInterface)
        {
            if (viewInterface is IClientAddView)
            {
                ((IClientAddView)viewInterface).SetParams(ActionToParentWindow);
                ((IClientAddView)viewInterface).ReturnView<Form>().ShowDialog();
            }
            if (viewInterface is IClientUpdateView)
            {
                ((IClientUpdateView)viewInterface).SetParams(selectedClient);
                ((IClientUpdateView)viewInterface).ReturnView<Form>().ShowDialog();
            }
            if (viewInterface is IClientDeleteView)
            {
                ((IClientDeleteView)viewInterface).SetParams(selectedClient);
                ((IClientDeleteView)viewInterface).ReturnView<Form>().ShowDialog();
            }
        }
    }
}
